package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.StudentBean;
import com.project.service.IStudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private IStudentService service;

    @RequestMapping("findByItem")
    public String findByItem(@RequestParam(value = "pageNO",defaultValue = "1") Integer pageNO,
                             String name,
                             LocalDate startDate,
                             LocalDate endDate,
                             HttpServletRequest req) {
        IPage ip = service.cutByItem(pageNO, name, startDate, endDate);
        req.setAttribute("ipage",ip);
        req.setAttribute("name",name);
        req.setAttribute("startDate",startDate);
        req.setAttribute("endDate",endDate);
        return "/index.html";
    }
    @RequestMapping("findById")
    public String findById(Integer id,HttpServletRequest req){
        StudentBean stu = service.findById(id);
        req.setAttribute("stu",stu);

        return "/update.html";
    }

    @RequestMapping("update")
    public String update(Integer id,String phone){
        service.update(id,phone);
        return "redirect:/student/findByItem";
    }
    @RequestMapping("add")
    public String add(String name,String birthday,String phone){
        System.out.println(name);
        service.add(new StudentBean(name,LocalDate.parse(birthday),phone));
        return "redirect:/student/findByItem";
    }
    @RequestMapping("del")
    public String del(Integer id){
        service.del(id);
        return "redirect:/student/findByItem";
    }
}
